/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.sxyc.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.modules.yckj.common.ThreadLocalUtil;
import io.renren.modules.yckj.dto.AddressDTO;
import io.renren.modules.yckj.enums.status.CommonStatus;
import io.renren.modules.yckj.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/yckj/address")
@Tag(name = "地址接口", description = "地址接口")
@AllArgsConstructor
public class ApiAddressController {

    private final AddressService addressService;

    @Login
    @GetMapping("list")
    @Operation(summary = "获取列表")
    @ApiResponse(content = @Content(mediaType = "application/json",
            schema = @Schema(implementation = AddressDTO.class)))
    public Result<List<AddressDTO>> list() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", ThreadLocalUtil.getUserId());
        List<AddressDTO> dtoList = addressService.list(map);
        return new Result<List<AddressDTO>>().ok(dtoList);
    }

    @Login
    @PostMapping("save")
    @Operation(summary = "添加")
    public Result<Boolean> save(@RequestBody AddressDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setUserId(ThreadLocalUtil.getUserId());
        dto.setUsername(ThreadLocalUtil.getUserName());
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());
        if (dto.getIsDefault().equals(CommonStatus.YES.getCode())) {
            //把是默认的更新成不是默认
            addressService.updateIsDefaultNo(dto.getUserId());
        }
        boolean b = addressService.save(dto);

        return new Result<Boolean>().ok(b);
    }

    @Login
    @PostMapping("update")
    @Operation(summary = "修改")
    public Result<Boolean> update(@RequestBody AddressDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        AddressDTO dbDto = addressService.get(dto.getId());
        boolean b = false;
        if (dbDto != null && dbDto.getUserId().equals(ThreadLocalUtil.getUserId())) {
            dto.setUpdateTime(new Date());
            if (dto.getIsDefault().equals(CommonStatus.YES.getCode())) {
                //把是默认的更新成不是默认
                addressService.updateIsDefaultNo(dbDto.getUserId());
            }

            b = addressService.update(dto);
        }
        return new Result<Boolean>().ok(b);
    }

    @Login
    @GetMapping("delete")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "主键", required = true)
    public Result<Boolean> delete(@RequestParam("id") Long id) {
        AddressDTO dbDto = addressService.get(id);
        boolean b = false;
        if (dbDto != null && dbDto.getUserId().equals(ThreadLocalUtil.getUserId()))
            b = addressService.deleteById(id);
        return new Result<Boolean>().ok(b);
    }
}